home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / 32SNIPIT.PAK / INMEMTBL.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  197 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // Inmemtbl.C
  4. #include "snipit.h"
  5.  
  6. static char szTblName[] = "INMEMTBL";
  7.  
  8. // Field Descriptor used in creating a table
  9. static SNIPFAR FLDDesc fldDesc[] = {
  10.               { // Field 1 - ALPHA
  11.                 1,            // Field number
  12.                 "ALPHA",      // Field name
  13.                 fldZSTRING,   // Field type
  14.                 fldUNKNOWN,   // Field subtype
  15.                 10,           // Field size
  16.                 0,            // Decimal places ( 0 )
  17.                               //     computed
  18.                 0,            // Offset in record ( 0 )
  19.                 11,           // Length in bytes  ( 0 )
  20.                 0,            // For Null bits    ( 0 )
  21.                 fldvNOCHECKS, // Validity checks   ( 0 )
  22.                 fldrREADWRITE // Rights
  23.               },
  24.               { // Field 2 - NUMERIC
  25.                 2, "NUMERIC", fldFLOAT, fldUNKNOWN,
  26.                 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE
  27.               },
  28.               { // Field 3 - MONEY
  29.                 3, "MONEY", fldFLOAT, fldstMONEY,
  30.                 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE
  31.               },
  32.               { // Field 4 - DATE
  33.                 4, "DATE", fldDATE, fldUNKNOWN,
  34.                 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE
  35.               },
  36.               { // Field 5 - SHORT
  37.                 5, "SHORT", fldINT16, fldUNKNOWN,
  38.                 0, 0, 0, 2, 0, fldvNOCHECKS, fldrREADWRITE
  39.               },
  40.               { // Field 6 - TIME
  41.                 6, "TIME", fldTIME, fldUNKNOWN,
  42.                 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE
  43.               },
  44.               { // Field 7 - TIMESTAMP
  45.                 7, "TIMESTAMP", fldTIMESTAMP, fldUNKNOWN,
  46.                 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE
  47.               },
  48.               { // Field 8 - LONG
  49.                 8, "LONG", fldINT32, fldUNKNOWN,
  50.                 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE
  51.               },
  52.               { // Field 9 - BOOL
  53.                 9, "BOOL", fldBOOL, fldUNKNOWN,
  54.                 0, 0, 0, 2, 0, fldvNOCHECKS, fldrREADWRITE
  55.               },
  56.               { // Field 10 - BYTES
  57.                 10, "BYTES", fldBYTES, fldUNKNOWN,
  58.                 20, 0, 0, 20, 0, fldvNOCHECKS, fldrREADWRITE
  59.               }
  60.              }; // Array of field descriptors
  61.  
  62. // The number of fields in the table
  63. static UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  64.  
  65. static DBIResult FillTable1(hDBICur hCur, FLOAT NumRecs);
  66.  
  67. //=====================================================================
  68. //  Function:
  69. //
  70. //          CreateAndFillInMemoryTbl();
  71. //  Description:
  72. //          This sample code will create an in-memory table, insert
  73. //          10 records into the table, then close the table.
  74. //
  75. //          There are the following limits for In-Memory table:
  76. //          Fields:         1024
  77. //          Max Rec Size:   16K
  78. //          Indexes:        0 (Cannot be indexed)
  79. //          Table Size:     512M
  80. //=====================================================================
  81. void
  82. CreateAndFillInMemoryTbl (void)
  83. {
  84.     hDBIDb      hDb;                // Handle to the database
  85.     hDBICur     hCur;               // Handle to the table
  86.     UINT16      uDispNumRecs = 10 ; // Number of records to add and
  87.                                     // display.
  88.     DBIResult   rslt;               // Return value from IDAPI functions
  89.  
  90.     Screen("*** Create/Open/Fill In-Memory Example ***\r\n");
  91.  
  92.     BREAK_IN_DEBUGGER();
  93.  
  94.     Screen("    Initializing IDAPI...");
  95.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  96.     {
  97.         Screen("\r\n*** End of Example ***");
  98.         return;
  99.     }
  100.  
  101.     Screen("    Setting the database directory...");
  102.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  103.     ChkRslt(rslt, "SetDirectory");
  104.  
  105.     Screen("    Creating the %s in-memory table...", szTblName);
  106.     rslt = DbiCreateInMemTable(hDb, szTblName, uNumFields, fldDesc, &hCur);
  107.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  108.     {
  109.         CloseDbAndExit(&hDb);
  110.         Screen("\r\n*** End of Example ***");
  111.         return;
  112.     }
  113.  
  114.     Screen("    Fill the %s table with random data...", szTblName);
  115.     FillTable1(hCur, uDispNumRecs);
  116.  
  117.     rslt = DbiSetToBegin(hCur);
  118.     ChkRslt(rslt, "SetToBegin");
  119.  
  120.     Screen("    Display the %s table which we just created...", szTblName);
  121.     DisplayInMemoryTable(hCur, uDispNumRecs);
  122.  
  123.     // You do not need to delete this table.  IDAPI will delete the
  124.     // table when it is closed.
  125.     Screen("\r\n    Close the %s table...", szTblName);
  126.     rslt = DbiCloseCursor(&hCur);
  127.     ChkRslt(rslt, "CloseCursor");
  128.  
  129.     Screen("    Close the database and exit IDAPI...");
  130.     CloseDbAndExit(&hDb);
  131.  
  132.     Screen("\r\n*** End of Example ***");
  133. }
  134.  
  135. //=====================================================================
  136. //  Function:
  137. //          FillTable1(hCur, NumRecs);
  138. //
  139. //  Input:  hCur        - The table cursor
  140. //          NumRecs     - The number of records to insert
  141. //
  142. //  Return: DBIResult - success?
  143. //
  144. //  Description:
  145. //          This function adds the specified number of records to
  146. //          the in-memory table.
  147. //=====================================================================
  148. DBIResult
  149. FillTable1 (hDBICur hCur, FLOAT NumRecs)
  150. {
  151.     DBIResult   rslt;           // Return value from IDAPI functions
  152.     FLOAT       fRecCount;      // Loop variable = count of records
  153.     UINT16      FldCntr;        // Field counter
  154.     pBYTE       pRecBuf;        // Pointer to the record buffer
  155.     CURProps    TblProps;       // Table descriptor
  156.  
  157.     Screen("    Inserting %.0f records into the table...", NumRecs);
  158.  
  159.     if (hCur)
  160.     {
  161.         //  Allocate a record buffer
  162.         rslt = DbiGetCursorProps(hCur, &TblProps);
  163.         ChkRslt(rslt, "GetCursorProps");
  164.  
  165.         pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize);
  166.         if (pRecBuf == NULL)
  167.         {
  168.             Screen("    Error - Out of memory");
  169.             return DBIERR_NOMEMORY;
  170.         }
  171.  
  172.         // Loop until the specified number of records have been written
  173.         // to the table.
  174.         for (fRecCount = 0; fRecCount < NumRecs; fRecCount++)
  175.         {
  176.             // Make sure we're starting with a clean record buffer
  177.             rslt = DbiInitRecord(hCur, pRecBuf);
  178.             ChkRslt(rslt, "InitRecord");
  179.  
  180.             for (FldCntr = 0; FldCntr < uNumFields; FldCntr++)
  181.             {
  182.                 // Put field data into the record buffer
  183.                 PutFieldSample(hCur, pRecBuf, (UINT16)(FldCntr + 1), &fldDesc[FldCntr]);
  184.             }
  185.  
  186.             // Append the record to the table...
  187.             rslt = DbiAppendRecord(hCur, pRecBuf);
  188.             ChkRslt(rslt, "InsertRecord");
  189.         }
  190.  
  191.         free((pCHAR) pRecBuf);
  192.     }
  193.  
  194.     return DBIERR_NONE;
  195. }
  196.  
  197.